<%@ page import="java.time.LocalDateTime" %><%--
  Created by IntelliJ IDEA.
  User: ADMIN
  Date: 2023/3/27
  Time: 21:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
    request.setCharacterEncoding("utf-8");
    //判断需要处理的逻辑//
    //       state = 1  插入 2/修改 3 删除

    Integer state = Integer.valueOf(request.getParameter("state"));
    String opereation = null;
    Integer departId = Integer.valueOf(session.getAttribute("depart").toString());


    Integer id = null;
    String username = null;
    String name= null;
    String phone = null;
    String createtime = null;
    String password = null;
    String updatetime= null;
    Integer depart =null;
    String avatar = null;


    if(state == 1){
        avatar = request.getParameter("avatar");
        username   =  request.getParameter("username");
        name = request.getParameter("name");
        phone = request.getParameter("phone");
        createtime = LocalDateTime.now().toString();
        password = request.getParameter("password");
        depart = Integer.valueOf(request.getParameter("depart"));
    }else if (state == 2){
        id = Integer.valueOf(request.getParameter("id"));
        username   =  request.getParameter("username");
        name = request.getParameter("name");
        phone = request.getParameter("phone");
        createtime = LocalDateTime.now().toString();
        password = request.getParameter("password");
        updatetime = LocalDateTime.now().toString();
        avatar = request.getParameter("avatar");

    }else {
        id = Integer.valueOf(request.getParameter("id"));
    }

    String sql = null;
   if(state == 1){
       //新增
       opereation = "新增用户";

      sql = "INSERT INTO user(username,password,name,phone,depart,avatar,createtime,updatetime) VALUES ( '"+username+"', '"+password+"', '"+name+"', '"+phone+"', "+depart+", '"+avatar+"', '"+createtime+"', '"+createtime+"')";
   }else if(state == 2){
       //修改
       if(departId ==11){
           depart = Integer.valueOf(request.getParameter("depart"));
           sql =" update user set username ='"+username+"',name='"+name+"',phone='"+phone+"',depart='"+depart+"',avatar='"+avatar+"' where id="+id;
       }else {
           sql =" update user set username ='"+username+"',name='"+name+"',phone='"+phone+"',password='"+password+"',avatar='"+avatar+"' where id="+id;
       }

       opereation = "修改用户";

   }else {
       opereation = "删除用户";
       //删除
       sql = "delete from user where id = "+id;
   }

    System.out.println(sql);
   //jdbc连接数据库

    //jdbc 操作
    String url = "jdbc:mysql://localhost:3306/carxianyu";
    String user = "root";
    String pwd = "root";
    int rs = 0;
    Connection conn=null;
    try {
        Class.forName("com.mysql.jdbc.Driver");//加载驱动
        conn = DriverManager.getConnection("jdbc:mysql://43.142.239.28:13307/carxianyu?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC","root","root");
        PreparedStatement a = conn.prepareStatement(sql);
        rs = a.executeUpdate(sql);
        System.out.println("执行了sql =====》"+sql);



        if (rs != 0){
            //
            if(departId == 11){
                out.println("<script type='text/javascript'>alert('"+opereation+"成功');window.location.href='userList.jsp';</script>");
            }else {
                out.println("<script type='text/javascript'>alert('"+opereation+"成功');window.location.href='/view/customer/customer/customerDetail.jsp';</script>");
            }

        }else{
            out.println("<script type='text/javascript'>alert('"+opereation+"失败');window.location.href='userList.jsp';</script>");
        }


    }catch (Exception exception){}finally {
        conn.close();
    }


%>
















<head>
    <title>处理cru的逻辑</title>
</head>
<body>

</body>
</html>
